package com.ny.dao.impl;

import com.ny.dao.ProductDao;
import entity.Product;
import until.SqlConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {

    Connection connection = SqlConnection.getCon();

    public ProductDaoImpl() throws SQLException, ClassNotFoundException {
    }
        @Override
        public List<Product> findAllProduct () throws SQLException {


            String sql = "select * from products";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            List<Product> productList = new ArrayList<>();
            while (resultSet.next()) {
                Product product = new Product();
                product.setProduct_id(resultSet.getInt("product_id"));
                product.setName(resultSet.getString("name"));
                product.setPrice(resultSet.getDouble("price"));
                product.setStock(resultSet.getInt("stock"));
                product.setCategory(resultSet.getString("category"));
                product.setDescription(resultSet.getString("description"));
                productList.add(product);
            }

            return productList;
        }
    }

